package ys.mihoyo.ymcc.web.controller;


import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ys.mihoyo.ymcc.result.JSONResult;
import ys.mihoyo.ymcc.result.PageList;
import ys.mihoyo.ymcc.domain.Employee;
import ys.mihoyo.ymcc.query.EmployeeQuery;
import ys.mihoyo.ymcc.service.IEmployeeService;

@RestController
@RequestMapping("/employee")
@Api(value = "EmployeeController", tags = {"员工管理"})
public class EmployeeController {

    @Autowired
    public IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    @ApiOperation(value = "保存和修改公用的",notes = "保存和修改公用的",response = JSONResult.class)
    public JSONResult saveOrUpdate(@ApiParam(name = "employee",required = true) @RequestBody Employee employee){
        if(employee.getId()!=null){
            employeeService.updateById(employee);
        }else{
            employeeService.insert(employee);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    @ApiOperation(value = "删除对象",notes = "删除对象",response = JSONResult.class)
    public JSONResult delete(@ApiParam(name = "id",required = true) @PathVariable("id") Long id){
        employeeService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "获取对象",notes = "获取对象",response = Employee.class)
    public JSONResult get(@ApiParam(name = "id",required = true) @PathVariable("id")Long id){
        return JSONResult.success(employeeService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(employeeService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    @ApiOperation(value = "带条件分页查询数据",notes = "带条件分页查询数据",response = PageList.class)
    public JSONResult page(@ApiParam(name = "query",required = true) @RequestBody EmployeeQuery query){
        Page<Employee> page = new Page<Employee>(query.getPage(),query.getRows());
        page = employeeService.selectPage(page);
        return JSONResult.success(new PageList<Employee>(page.getTotal(),page.getRecords()));
    }
}
